const _ = require("../import");




/**
 * @api {post} /user/login  login
 * @apiDescription  用户登录的接口，注意用户登录成功后会在result中包含token参数，请前端本地保存token。
 * @apiName login
 * @apiGroup user
 * @apiParam {string} name 用户名
 * @apiParam {string} pwd  用户密码
 * @apiSuccessExample {json} 请求成功的返回:
 *  {
 *      "status" : 200,
 *      "message" : "请求成功"
 *      "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
 *  }
 * @apiErrorExample {json} 请求失败的返回
 * {
 *   "status": 404,
 *   "message" : "请求错误"
 *   "result": null
 * }
 * @apiSampleRequest http://127.0.0.1:9090/user/login
 * @apiVersion 1.0.0
 */
_.router.post('/login', async function (req, res, next) {

    var username = req.body.name,
        userpwd = req.body.pwd;

    var data = await _.db.query({
        sql: _.config.sql.userLogin,
        data: [username,_.utils.Md5Password(userpwd)]
    })

    if (data.length != 0 ){
        var token = _.jwt.sign({
            id: data[0].user_id,
            username: data[0].user_name
        }, _.config.jwt, { expiresIn: '3h' });
        res.json({
            status: 200,
            message: '登录成功',
            result: {
              token: token
            }
        });
    } else {
        res.json({
            status: 404,
            message: '账户名或密码有误',
            result: null
        });
    }

});


/**
 * @api {post} /user/add  add
 * @apiDescription  用户注册的接口
 * @apiName add
 * @apiGroup user
 * @apiParam {string} name 用户名
 * @apiParam {string} pwd  用户密码
 * @apiParam {string} phone  用户手机号
 * @apiSuccessExample {json} 请求成功的返回:
 *  {
 *      "status" : 200,
 *      "message" : "注册成功"
 *      "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
 *  }
 * @apiErrorExample {json} 请求失败的返回
 * {
 *   "status": 404,
 *   "message" : "注册失败"
 *   "result": null
 * }
 * @apiSampleRequest http://127.0.0.1:9090/user/add
 * @apiVersion 1.0.0
 */
_.router.post('/add', async function (req, res, next) {

    var username = req.body.name,
        userpwd = req.body.pwd,
        userphone = req.body.phone;

    var data = await _.db.query({
        sql: _.config.sql.addUser,
        data: [username,_.utils.Md5Password(userpwd),userphone, _.utils.getTime()]
    })

    if(data.hasOwnProperty('insertId')) {
        res.json({
            status: 200,
            message: '注册成功',
            result: {}
        });
    } else {
        res.json({
            status: 404,
            message: '注册失败',
            result: null
        });
    }
});



/**
 * @api {post} /user/like  like
 * @apiDescription  用户在详情页收藏商品使用的接口
 * @apiName like
 * @apiGroup user
 * @apiParam {number} shopid 需要被收藏商品的id，这里是商品的 wares_id 字段
 * @apiSuccessExample {json} 请求成功的返回:
 *  {
 *      "status" : 200,
 *      "message" : "收藏成功"
 *      "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
 *  }
 * @apiErrorExample {json} 请求失败的返回
 * {
 *   "status": 404,
 *   "message" : "抱歉，请勿重复收藏"
 *   "result": null
 * }
 * @apiSampleRequest http://127.0.0.1:9090/user/like
 * @apiVersion 1.0.0
 */
_.router.post('/like', async function (req, res, next) {

  var like_shop = req.body.shopid;
  var IsLike = await _.db.query({
    sql: _.config.sql.selectLike,
    data: [like_shop,_.jwt.decode(req.headers.token).id]
  });

  if(IsLike.length != 0) {
    res.json({
      status: 404,
      message: '抱歉，请勿重复收藏',
      result: {}
    });
  } else {
    var data = await _.db.query({
      sql: _.config.sql.likeShop,
      data: [like_shop, _.jwt.decode(req.headers.token).id]
    });

    if(data.hasOwnProperty('insertId')) {
      res.json({
        status: 200,
        message: '收藏成功',
        result: {}
      });
    } else {
      res.json({
        status: 404,
        message: '收藏失败',
        result: null
      });
    }
  }
});



/**
 * @api {post} /user/likeList  likeList
 * @apiDescription  获取用户收藏的商品（不需要参数，后端会解密请求头token获取user_id）
 * @apiName likeList
 * @apiGroup user
 * @apiSuccessExample {json} 请求成功的返回:
 *  {
 *      "status" : 200,
 *      "message" : "请求成功"
 *      "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
 *  }
 * @apiErrorExample {json} 请求失败的返回
 * {
 *   "status": 404,
 *   "message" : "请求失败"
 *   "result": null
 * }
 * @apiSampleRequest http://127.0.0.1:9090/user/likeList
 * @apiVersion 1.0.0
 */
_.router.post('/likeList', async function(req, res, next) {

  var data = await _.db.query({
    sql: _.config.sql.getLikeList,
    data: [ _.jwt.decode(req.headers.token).id ]
  });

  res.json({
    status: 200,
    message: '请求成功',
    result: data
  });

});



/**
 * @api {post} /user/getUserAddress  getUserAddress
 * @apiDescription  获取用户的收货地址（不需要参数，传空对象 {} 即可 ）
 * @apiName getUserAddress
 * @apiGroup user
 * @apiSuccessExample {json} 请求成功的返回:
 *  {
 *      "status" : 200,
 *      "message" : "请求成功"
 *      "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
 *  }
 * @apiErrorExample {json} 请求失败的返回
 * {
 *   "status": 200,
 *   "message" : "没有收货地址"
 *   "result": null
 * }
 * @apiSampleRequest http://127.0.0.1:9090/user/getUserAddress
 * @apiVersion 1.0.0
 */
_.router.post('/getUserAddress', async function(req, res, next) {

  var data = await _.db.query({
    sql: _.config.sql.getUserAddress,
    data: [ _.jwt.decode(req.headers.token).id ]
  });

  if (data[0].user_address == null || data[0].user_address.length == 0) {
    res.json({
      status: 200,
      message: '没有收货地址',
      result: {}
    });
  } else {
    res.json({
      status: 200,
      message: '请求成功',
      result: data
    });
  }

});


module.exports = _.router;
